#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Optional


class ArrayQueue:
    """用数组实现的队列"""

    def __init__(self, size):
        """
        :param size: 队列大小
        """
        self.items: list = [None] * size
        self.size = size
        self.head = 0  # 队头下标
        self.tail = 0  # 队尾下标

    def enqueue(self, item) -> bool:
        """入队"""
        if self.tail == self.size:
            if self.head == 0:
                print("队列已满")
                return False
            # 数据迁移
            for i in range(self.head, self.tail):
                self.items[i - self.head] = self.items[i]
            # 数据迁移完后要更新head和tail
            self.tail -= self.head
            self.head = 0
        self.items[self.tail] = item
        self.tail += 1
        return True

    def dequeue(self) -> Optional[str]:
        """出队"""
        if self.head == self.tail:
            print("队列为空队列")
            return None

        item = self.items[self.head]
        self.head += 1
        return item

    def __repr__(self) -> str:
        return str(self.items[self.head:self.tail])


if __name__ == '__main__':
    q = ArrayQueue(10)
    for i in range(10):
        q.enqueue(i)
    print(q)

    for _ in range(3):
        q.dequeue()
    print(q)

    q.enqueue(7)
    q.enqueue(8)
    print(q)
